/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package prototype.santana;


import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author Eric
 */
public class SimulationGUI extends javax.swing.JFrame {

     public int START_TIME= 15*60;
    
    int time ;
    @Override
    public void setVisible(boolean b) {
        super.setVisible(b);
        if(b){
            jTabbedPane1.setSelectedIndex(0);
            time = START_TIME;
            //JPanel h = new Jp
            
        }
    }
    

    public SimulationManager manager;
    /**
     * Creates new form PrototypeGUI2
     */
    public SimulationGUI(SimulationManager managerr) {
        initComponents();
        setLocation(
                (getToolkit().getScreenSize().width-getWidth())/2,
                (getToolkit().getScreenSize().height-getHeight())/2);
        this.manager = managerr;
        ActionListener timerListener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if(time>0 && isVisible()){
                time--;
                timeLabel.setText(time/60+":"+(
                        (time -(time/60)*60)  >=10 ?""+
                        (time-(time/60)*60):
                        "0"+(time-(time/60)*60)));
                }
                if(time ==0 && isVisible()){
                    JOptionPane.showMessageDialog(rootPane,
                            "TIME OVER!\n"
                            + "You will be logged off automatically");
                    setVisible(false);
                    manager.state = SimulationManager.LOGIN_STATE;
                }
                if(!isVisible())
                    time=START_TIME;
               
            }
        };
        Timer timer = new Timer(1000,timerListener);
        
        timer.start();
        ((DefaultTableModel)documentationTable.getModel()).addRow(new Object[]{"12-12-12","5:00PM","SampleMultipleLines\n\n Eric S "});
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        mar_Panel = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        roomNumLabel = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        patientNameLabel = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        diagnosisLabel = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        allergiesLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        marTable = new javax.swing.JTable();
        giveMedicationButton = new javax.swing.JButton();
        documentation_Panel = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane5 = new javax.swing.JScrollPane();
        documentationTable = new javax.swing.JTable();
        insertNewNarrativeButton = new javax.swing.JButton();
        viewSelectedNarrativeButton = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        editNarrativeButton = new javax.swing.JButton();
        jcaho_Panel = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jcahoLabel = new javax.swing.JLabel();
        submit_Panel = new javax.swing.JPanel();
        jLabel13 = new javax.swing.JLabel();
        submitButton = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        timeLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Prototype2GUI");
        setAlwaysOnTop(true);
        setBackground(new java.awt.Color(0, 0, 0));
        setResizable(false);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setText("Room:");

        roomNumLabel.setText("number");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel5.setText("Name:");

        patientNameLabel.setText("patientName");

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel7.setText("Diagnosis:");

        diagnosisLabel.setText("jLabel8");

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel9.setText("Allergies:");

        allergiesLabel.setText("jLabel10");

        marTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"Insulin", null, ""},
                {"Morphine", null, null},
                {"Cocaine", null, null}
            },
            new String [] {
                "Medication", "Dose", "Hour Due"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Object.class, java.lang.Object.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        marTable.setRowHeight(45);
        jScrollPane1.setViewportView(marTable);

        giveMedicationButton.setText("Give Medication");
        giveMedicationButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                giveMedicationButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout mar_PanelLayout = new javax.swing.GroupLayout(mar_Panel);
        mar_Panel.setLayout(mar_PanelLayout);
        mar_PanelLayout.setHorizontalGroup(
            mar_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mar_PanelLayout.createSequentialGroup()
                .addGroup(mar_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mar_PanelLayout.createSequentialGroup()
                        .addGap(40, 40, 40)
                        .addGroup(mar_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(mar_PanelLayout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(diagnosisLabel))
                            .addGroup(mar_PanelLayout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(patientNameLabel)))
                        .addGap(123, 123, 123)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(roomNumLabel)
                        .addGap(61, 61, 61)
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(allergiesLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(giveMedicationButton))
                    .addGroup(mar_PanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 1005, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addGap(1158, 1158, 1158))
        );
        mar_PanelLayout.setVerticalGroup(
            mar_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mar_PanelLayout.createSequentialGroup()
                .addGap(303, 303, 303)
                .addGroup(mar_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(patientNameLabel)
                    .addComponent(jLabel9)
                    .addComponent(jLabel3)
                    .addComponent(roomNumLabel)
                    .addComponent(allergiesLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(mar_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(diagnosisLabel))
                .addContainerGap())
            .addGroup(mar_PanelLayout.createSequentialGroup()
                .addGap(0, 49, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(giveMedicationButton)
                .addGap(232, 232, 232))
        );

        jTabbedPane1.addTab("MAR", mar_Panel);

        documentationTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Date", "Time", "Narrative", "Follow Up", "Initialls"
            }
        ));
        documentationTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        documentationTable.setName("sample");
        documentationTable.getTableHeader().setReorderingAllowed(false);
        jScrollPane5.setViewportView(documentationTable);
        documentationTable.getColumnModel().getColumn(0).setMinWidth(60);
        documentationTable.getColumnModel().getColumn(0).setPreferredWidth(60);
        documentationTable.getColumnModel().getColumn(0).setMaxWidth(60);
        documentationTable.getColumnModel().getColumn(1).setMinWidth(90);
        documentationTable.getColumnModel().getColumn(1).setPreferredWidth(90);
        documentationTable.getColumnModel().getColumn(1).setMaxWidth(90);
        documentationTable.getColumnModel().getColumn(2).setResizable(false);
        documentationTable.getColumnModel().getColumn(2).setPreferredWidth(695);
        documentationTable.getColumnModel().getColumn(3).setMinWidth(80);
        documentationTable.getColumnModel().getColumn(3).setPreferredWidth(80);
        documentationTable.getColumnModel().getColumn(3).setMaxWidth(80);
        documentationTable.getColumnModel().getColumn(4).setMinWidth(50);
        documentationTable.getColumnModel().getColumn(4).setPreferredWidth(50);
        documentationTable.getColumnModel().getColumn(4).setMaxWidth(50);

        insertNewNarrativeButton.setText("Insert a new Narrative");
        insertNewNarrativeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                insertNewNarrativeButtonActionPerformed(evt);
            }
        });

        viewSelectedNarrativeButton.setText("ViewSelectedNarrative");
        viewSelectedNarrativeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                viewSelectedNarrativeButtonActionPerformed(evt);
            }
        });

        jButton1.setText("Delete the Selected Narrative");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        editNarrativeButton.setText("Edit Selected Narrative");
        editNarrativeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editNarrativeButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(insertNewNarrativeButton)
                        .addGap(28, 28, 28)
                        .addComponent(viewSelectedNarrativeButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(editNarrativeButton)))
                .addContainerGap())
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 985, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 32, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 233, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(insertNewNarrativeButton)
                    .addComponent(viewSelectedNarrativeButton)
                    .addComponent(editNarrativeButton))
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addGap(135, 135, 135))
        );

        jScrollPane2.setViewportView(jPanel4);

        javax.swing.GroupLayout documentation_PanelLayout = new javax.swing.GroupLayout(documentation_Panel);
        documentation_Panel.setLayout(documentation_PanelLayout);
        documentation_PanelLayout.setHorizontalGroup(
            documentation_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2)
        );
        documentation_PanelLayout.setVerticalGroup(
            documentation_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(documentation_PanelLayout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 79, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Documentation", documentation_Panel);

        jcahoLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/JAHCO.png"))); // NOI18N
        jScrollPane4.setViewportView(jcahoLabel);

        javax.swing.GroupLayout jcaho_PanelLayout = new javax.swing.GroupLayout(jcaho_Panel);
        jcaho_Panel.setLayout(jcaho_PanelLayout);
        jcaho_PanelLayout.setHorizontalGroup(
            jcaho_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jcaho_PanelLayout.createSequentialGroup()
                .addGap(333, 333, 333)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 456, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(230, Short.MAX_VALUE))
        );
        jcaho_PanelLayout.setVerticalGroup(
            jcaho_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jcaho_PanelLayout.createSequentialGroup()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 552, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 1, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("JCAHO", jcaho_Panel);

        jLabel13.setText("By clicking the button below you complete the Simulation");

        submitButton.setText("Submit");
        submitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                submitButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout submit_PanelLayout = new javax.swing.GroupLayout(submit_Panel);
        submit_Panel.setLayout(submit_PanelLayout);
        submit_PanelLayout.setHorizontalGroup(
            submit_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(submit_PanelLayout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(submit_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(submitButton)
                    .addComponent(jLabel13))
                .addContainerGap(725, Short.MAX_VALUE))
        );
        submit_PanelLayout.setVerticalGroup(
            submit_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(submit_PanelLayout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(jLabel13)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(submitButton)
                .addContainerGap(485, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Submit", submit_Panel);

        jButton2.setText("Logout");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("Time Left:");

        timeLabel.setText("15:00");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(timeLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 694, Short.MAX_VALUE)
                .addComponent(jButton2)
                .addGap(156, 156, 156))
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 581, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(timeLabel))
                .addContainerGap(43, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void insertNewNarrativeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_insertNewNarrativeButtonActionPerformed

        DateFormat dateFormat = new SimpleDateFormat("MM/dd/yy");
       DateFormat dayFormat = new SimpleDateFormat("HH:mma");
        ((DefaultTableModel)documentationTable.getModel()).addRow(new String[]{
                    dateFormat.format(Calendar.getInstance(TimeZone.getDefault()).getTime()),
                    dayFormat.format(Calendar.getInstance(TimeZone.getDefault()).getTime())});
      //  documentationTable.isfo
    }//GEN-LAST:event_insertNewNarrativeButtonActionPerformed

    private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_submitButtonActionPerformed
        // TODO add your handling code here:
        setVisible(false);
        SimulationManager.state=SimulationManager.LOGIN_STATE;
    }//GEN-LAST:event_submitButtonActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        setVisible(false);
        SimulationManager.state = SimulationManager.LOGIN_STATE;
    }//GEN-LAST:event_jButton2ActionPerformed

    private void viewSelectedNarrativeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_viewSelectedNarrativeButtonActionPerformed
        // TODO add your handling code here:
        if(documentationTable.getSelectedRow()<0)
            JOptionPane.showMessageDialog(this, "Please Select a Narrative");
        else{
            JDialog t = new JDialog(this);
            t.setLocation(
                (t.getToolkit().getScreenSize().width-getWidth())/2,
                (t.getToolkit().getScreenSize().height-getHeight())/2);
            
            t.setTitle( patientNameLabel.getText()+": " +(String)documentationTable.
                    getValueAt(documentationTable.getSelectedRow(),0 )+" at "+
                    (String)documentationTable.
                    getValueAt(documentationTable.getSelectedRow(),1 ));
            t.setSize(400, 300);
            
            JTextPane t2 = new JTextPane();
            t2.setEditable(false);
            JScrollPane pane = new JScrollPane();
            pane.setViewportView(t2);
            t2.setText((String)documentationTable.
                    getValueAt(documentationTable.getSelectedRow(),2 ));
            t.add(pane);
            
            
            t.setVisible(true);
            t.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        }
    }//GEN-LAST:event_viewSelectedNarrativeButtonActionPerformed

    private void giveMedicationButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_giveMedicationButtonActionPerformed

        if(marTable.getSelectedRow()<0)
            JOptionPane.showMessageDialog(this, "Select a Medication before giving");
        else{
             MedicationDialog dialog= new MedicationDialog(this, true,documentationTable.getModel());
        }
    }//GEN-LAST:event_giveMedicationButtonActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        if(!(documentationTable.getSelectedRow()<0))
            ((DefaultTableModel)documentationTable.getModel()).removeRow(documentationTable.getSelectedRow());
            
    }//GEN-LAST:event_jButton1ActionPerformed

    private void editNarrativeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editNarrativeButtonActionPerformed
        if(documentationTable.getSelectedRow()<0)
            JOptionPane.showMessageDialog(this, "Please Select a Narrative");
        else{
            final JDialog t = new JDialog(this);
            t.setLayout(new  GridLayout(1, 2));
            
            t.setSize(800, 300);
            t.setLocation(
                (t.getToolkit().getScreenSize().width-getWidth())/2,
                (t.getToolkit().getScreenSize().height-getHeight())/2);
            
            t.setTitle( patientNameLabel.getText()+": " +(String)documentationTable.
                    getValueAt(documentationTable.getSelectedRow(),0 )+" at "+
                    (String)documentationTable.
                    getValueAt(documentationTable.getSelectedRow(),1 ));
            
            
            JTextPane t2 = new JTextPane();
            t2.setSize(350,300 );
            
            final JTextPane t3 = t2;
            
            
            JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
            //t2.setEditable(false);
            JScrollPane pane = new JScrollPane();
            pane.setViewportView(t2);
            t2.setText((String)documentationTable.
                    getValueAt(documentationTable.getSelectedRow(),2 ));
            
            t.add(pane);
            
            JButton saveButton = new JButton("Save");
            saveButton.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    documentationTable.setValueAt((Object)t3.getText(), documentationTable.getSelectedRow(), 2);
                    t.dispose();
                }
            });
            panel.add(saveButton);
            JButton cancelButton = new JButton("Cancel");
            cancelButton.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    t.dispose();
                }
            });
            panel.add(cancelButton);
            
            t.add(panel);
            
            
            t.setVisible(true);
            t.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        }
    }//GEN-LAST:event_editNarrativeButtonActionPerformed

    
    
    
    /**
 *
 * @author Eric
 */
private class MedicationDialog extends javax.swing.JDialog {
    /**
     * Creates new form MedicationDialog
     */
    public MedicationDialog(Frame parent, boolean modal, TableModel recordingTable) {
        super(parent, modal);
        initComponents();
        setLocation(
                (getToolkit().getScreenSize().width-getWidth())/2,
                (getToolkit().getScreenSize().height-getHeight())/2);
        
      medicationNameLabel.setText(
              (String)((SimulationGUI)parent).
              getMarTable().getModel().
              getValueAt(((SimulationGUI)parent).getMarTable().getSelectedRow(), 0));
      
      setVisible(true);
        
    }

        /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        giveMedicationConfirmButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        medicationNameLabel = new javax.swing.JLabel();
        amountTextField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        unitsTextField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        notesTextPane = new javax.swing.JTextPane();
        cancelButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        followUpTextField = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        initialsTextField = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        giveMedicationConfirmButton.setText("Give Medication");
        giveMedicationConfirmButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                giveMedicationConfirmButtonActionPerformed(evt);
            }
        });
        
        amountTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                amountTextFieldKeyPressed(evt);
            }
        });
        
        
        getContentPane().add(giveMedicationConfirmButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(506, 108, -1, -1));

        jLabel1.setText("Amount");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 10, -1, -1));

        medicationNameLabel.setText("Medication Name");
        getContentPane().add(medicationNameLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 33, -1, -1));

        amountTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                amountTextFieldActionPerformed(evt);
            }
        });
        getContentPane().add(amountTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 30, 60, -1));

        jLabel3.setText("Units");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(283, 11, -1, -1));

        unitsTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                unitsTextFieldActionPerformed(evt);
            }
        });
        getContentPane().add(unitsTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(283, 30, 121, -1));

        jLabel4.setText("Notes");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 96, -1, -1));

        jScrollPane1.setViewportView(notesTextPane);

        getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 116, 363, 173));

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });
        getContentPane().add(cancelButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(506, 236, 107, -1));

        jLabel2.setText("Follow up");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 11, -1, -1));
        getContentPane().add(followUpTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 30, 114, -1));

        jLabel5.setText("Initials");
        getContentPane().add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(568, 11, -1, -1));
        getContentPane().add(initialsTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(564, 30, 49, -1));

        jLabel6.setText("Medication");
        getContentPane().add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, -1, -1));

        pack();
        
        amountTextField.addPropertyChangeListener(new PropertyChangeListener() {

                @Override
                public void propertyChange(PropertyChangeEvent evt) {
                    Integer i;
                    try{
                    i = Integer.parseInt(amountTextField.getText());
                    }
                    catch(Exception e){
                        i = null;

                    }
                    if(i==null)
                        amountTextField.setText("");
                }
            });
    }// </editor-fold>
    
    

    private void amountTextFieldKeyPressed(KeyEvent evt) {
        
        if( !((evt.getKeyChar()>=java.awt.event.KeyEvent.VK_0) && 
           (evt.getKeyChar()<=java.awt.event.KeyEvent.VK_9)) &&
           evt.getKeyChar()!=java.awt.event.KeyEvent.VK_BACK_SPACE &&
           amountTextField.isFocusOwner()){
            
        
            
            amountTextField.
                    setText
           ( amountTextField.getText().substring(0, (amountTextField.getText().length()>0 ? amountTextField.getText().length()-1:0)));
        
        }
        
    }
    
    private void giveMedicationConfirmButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                            
        // TODO add your handling code here:
       DateFormat dateFormat = new SimpleDateFormat("MM/dd/yy");
       DateFormat dayFormat = new SimpleDateFormat("HH:mma");

       ((DefaultTableModel)documentationTable.getModel()).addRow(
                new String[]{
                    dateFormat.format(Calendar.getInstance(TimeZone.getDefault()).getTime()),
                    dayFormat.format(Calendar.getInstance(TimeZone.getDefault()).getTime()),
"Medication Given: "+medicationNameLabel.getText()+ " Dose:"+amountTextField.getText()+" "+unitsTextField.getText()+" \n\nNotes:\n"+notesTextPane.getText(),
                followUpTextField.getText(),
                initialsTextField.getText()});
        this.dispose();
    }                                                           

    private void unitsTextFieldActionPerformed(java.awt.event.ActionEvent evt) {                                               

    }                                              

    private void amountTextFieldActionPerformed(java.awt.event.ActionEvent evt) { 
       Integer i;
        try{
         i = Integer.parseInt(amountTextField.getText());
        }
        catch(Exception e){
            i = null;
            
        }
        if(i==null)
            amountTextField.setText("");
    
    }                                               

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {
       dispose();
    }
    // Variables declaration - do not modify
    private javax.swing.JTextField amountTextField;
    private javax.swing.JButton cancelButton;
    private javax.swing.JTextField followUpTextField;
    private javax.swing.JButton giveMedicationConfirmButton;
    private javax.swing.JTextField initialsTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel medicationNameLabel;
    private javax.swing.JTextPane notesTextPane;
    private javax.swing.JTextField unitsTextField;
    private javax.swing.JLabel jLabel6;
    // End of variables declaration
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(SimulationGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(SimulationGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(SimulationGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(SimulationGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new SimulationGUI(new SimulationManager()).setVisible(true);
            }
        });
    }

    public void setMarTable(JTable marTable) {
        this.marTable = marTable;
    }

    public void setDocumentationTable(JTable documentationTable) {
        this.documentationTable = documentationTable;
    }
    
    

    public JTable getMarTable() {
        return marTable;
    }

    public JTable getDocumentationTable() {
        return documentationTable;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel allergiesLabel;
    private javax.swing.JLabel diagnosisLabel;
    private javax.swing.JTable documentationTable;
    private javax.swing.JPanel documentation_Panel;
    private javax.swing.JButton editNarrativeButton;
    private javax.swing.JButton giveMedicationButton;
    private javax.swing.JButton insertNewNarrativeButton;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JLabel jcahoLabel;
    private javax.swing.JPanel jcaho_Panel;
    private javax.swing.JTable marTable;
    private javax.swing.JPanel mar_Panel;
    private javax.swing.JLabel patientNameLabel;
    private javax.swing.JLabel roomNumLabel;
    private javax.swing.JButton submitButton;
    private javax.swing.JPanel submit_Panel;
    private javax.swing.JLabel timeLabel;
    private javax.swing.JButton viewSelectedNarrativeButton;
    // End of variables declaration//GEN-END:variables
}
